# coding: utf8

"""
    单分派singledispatch绑定多个函数到主函数
    仅根据第一个参数的类型进行判断分派，所以称为单分派

    Design Pattern:
    ----------------
    @singledispatch
    def basefun(arg):
       [base fun code]

    @basefun(type)
    def _(arg):
        [dispatch fun code]
    ---------------

    >>> add12(2, [1, 2])
    [1, 2, 1, 2]
    >>> add12([2], [1, 2])
    [3]
    >>> add12((2, 3), [1, 2])
    [(2+1j), (3+2j)]
"""


from functools import singledispatch


@singledispatch
def add12(x, y):
    return x * y


@add12.register(list)
def _(x, y):
    return [v1+v2 for v1, v2 in zip(x, y)]


@add12.register
def _(x: tuple, y):
    """
    support first augment annotation in Python 3.7 or later
    """
    return [v1+v2*1j for v1, v2 in zip(x, y)]
